package com.fanjia.demologin.util;


import lombok.Data;

/**
 * @author: csf
 * @file: ResultUtil.java
 * @time: 2020\4\13 0013 9:28
 * @desc:
 */

@Data
public class ResultUtil {
    private int code;
    private String msg;
    private Object data;

    /**
     * @param data 返回的数据
     * @return 请求成功，msg-成功
     */
    public static ResultUtil success(Object data) {
        ResultUtil result = new ResultUtil();
        result.setCode(Code.SUCCESS.getCode());
        result.setMsg(Code.SUCCESS.getText());
        result.setData(data);
        return result;
    }

    /**
     * @param data 返回的数据
     * @return 请求成功，msg自定义
     */
    public static ResultUtil success(Object data, String msg) {
        ResultUtil result = new ResultUtil();
        result.setCode(Code.SUCCESS.getCode());
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    /**
     * @return 请求成功，返回null, msg-成功
     */
    public static ResultUtil success() {
        return success(null);
    }

    /**
     * @param msg 失败
     * @return 请求失败
     */
    public static ResultUtil error(String msg) {
        ResultUtil result = new ResultUtil();
        result.setCode(Code.ERROR.getCode());
        result.setMsg(msg);
        return result;
    }

    /**
     * @param code Code
     * @return 请求失败
     */
    public static ResultUtil error(Code code) {
        ResultUtil result = new ResultUtil();
        result.setCode(code.getCode());
        result.setMsg(code.getText());
        return result;
    }

    /**
     * @param code Code
     * @param msg  错误信息
     * @return 请求失败，自定义错误码
     */
    public static ResultUtil error(Integer code, String msg) {
        ResultUtil result = new ResultUtil();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
}
